package com.probiz.estoresa.system.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;

import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.common.model.system.SelfCollectionCentre;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.system.service.RegionManager;
import com.probiz.estore.system.service.SelfCollectionCentreManager;

public class SelfCollectionCentreController extends GenericStoreAdminController<SelfCollectionCentre> {
    private SelfCollectionCentreManager selfCollectionCentreManager = null;
    private RegionManager regionManager = null;

    public void setSelfCollectionCentreManager(SelfCollectionCentreManager inMgr) {
        this.selfCollectionCentreManager = inMgr;
    }
    

	public void setRegionManager(RegionManager regionManager) {
		this.regionManager = regionManager;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(SelfCollectionCentre entity) {
		return entity.getSelfCollectionCentreName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = selfCollectionCentreManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(selfCollectionCentreName)");
		sortMap.put("2", "convert_gbk(regionFullName)");
		sortMap.put("3", "status");
		sortMap.put("4", "createTime");
		sortMap.put("5", "updateTime");
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, SelfCollectionCentre entity, BindException errors) {
		/*
		 * 在新增的时候，要判断公司名称是否存在 在修改的时候，要判断修改的名称是否存在（但不需要与自己对比）
		 */
		String msgKey = "selfCollectionCentre.isExisted";
		boolean isExist = false;
		if (entity.getSelfCollectionCentreId() == null) {// 新增
			isExist = this.selfCollectionCentreManager.isExistedSelfCollectionCentre(entity);
		} else {// 修改
			isExist = this.selfCollectionCentreManager.isExistedSelfCollectionCentreForUpdate(entity);
		}
		if (isExist) {
			errors.rejectValue("selfCollectionCentreName", msgKey);
		}	
		
		//设置regionFullName
		Region region = regionManager.getById(entity.getRegionId());
		entity.setRegionFullName(region.getFullName());
	}

}
